package com.test.services;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.comment.FightBean;
import com.test.comment.JudgeBean;
import com.test.comment.SingerScore;
import com.test.dao.UserDao;

@Service
public class ShowServices {
   @Autowired
   UserDao userDao;

public List<SingerScore> findsingerscore() {
	Logger logger = Logger.getLogger(getClass());
	FightBean fightBean = userDao.kai();
	List<JudgeBean> judgeBeansone = userDao.findjudge(fightBean.getSidOne(),fightBean.getRid());
	int sin1 = userDao.finduser(fightBean.getSidOne(),fightBean.getRid());
	int sintotal1 = userDao.findusertotal(fightBean.getRid());
	
	List<JudgeBean> judgeBeansone2 = userDao.findjudge(fightBean.getSidTwo(),fightBean.getRid());
    int sin2 = userDao.finduser(fightBean.getSidTwo(),fightBean.getRid());
	int sintotal2 = userDao.findusertotal(fightBean.getRid());
	
	int t1=0;
	for(int i=0;i<judgeBeansone.size();i++) {
		t1= t1+Integer.parseInt(judgeBeansone.get(i).getSinger_score());
	}
	int judge = t1/judgeBeansone.size();
	DecimalFormat df = new DecimalFormat("0.00");
	
	String userscore=df.format((float)sin1/sintotal1);
	double user  = Double.valueOf(userscore);
	int num1 = (int)(user*100);
	
    
    int t2=0;
	for(int i=0;i<judgeBeansone2.size();i++) {
		t2= t2+Integer.parseInt(judgeBeansone2.get(i).getSinger_score());
	}
	
	int judge2 = t2/judgeBeansone2.size();
	
	String userscore2 = df.format((float)sin2/sintotal2);
	double user2  = Double.valueOf(userscore2);
	int num2 = (int)(user2*100);
	
	String name1 = userDao.SingerName(fightBean.getSidOne());
	String name2 = userDao.SingerName(fightBean.getSidTwo());
	SingerScore singerScore1 = new SingerScore();
	singerScore1.setSingername(name1);
	singerScore1.setNum(Integer.toString(judge+num1));
	SingerScore singerScore2 = new SingerScore();
	singerScore2.setSingername(name2);
	singerScore2.setNum(Integer.toString(judge2+num2));
	List<SingerScore> singerScores = new ArrayList<>();
	singerScores.add(singerScore1);
	singerScores.add(singerScore2);
    
	return singerScores;
}
}
